package test.threads.queue.unstressed;

import java.util.Arrays;

import com.lmax.disruptor.EventFactory;
import net.jcip.annotations.NotThreadSafe;
import test.helpers.Config;
import test.threads.queue.common.IUsefulWorkEmulator;
import test.threads.queue.common.IUsefulWorkFactory;
import test.threads.queue.stressed.cpu.LongValueMessage;

/**
 * @author cheremin
 * @since 02.03.12,  15:41
 */
@NotThreadSafe
public class DummyWasterFactory implements IUsefulWorkFactory<LongValueMessage> {


    private final EventFactory<LongValueMessage> factory;
    private final boolean cacheMessages;

    public DummyWasterFactory( final Config config ) {
        cacheMessages = config.getAsBoolean( "payload.cache-messages", true );
        if ( cacheMessages ) {
            final int size = config.getAsInt( "task.buffer-size" );
            factory = EventPool.wrap( LongValueMessage.FACTORY, size );
        } else {
            factory = LongValueMessage.FACTORY;
        }
    }

    @Override
    public IUsefulWorkEmulator<LongValueMessage>[] createEmulators( final int count ) {
        final IUsefulWorkEmulator<LongValueMessage>[] emulators = new IUsefulWorkEmulator[count];
        final NoTimeWaster cpuTimeWaster = new NoTimeWaster();
        Arrays.fill( emulators, cpuTimeWaster );
        return emulators;
    }


    @Override
    public LongValueMessage newInstance() {
        final LongValueMessage message = factory.newInstance();
        return message;
    }

    @Override
    public String toString() {
        return "none[" + ( cacheMessages ? "cache" : "" ) + "]";
    }

    /**
     * @author cheremin
     * @since 27.02.12,  13:42
     */
    private static class NoTimeWaster implements IUsefulWorkEmulator<LongValueMessage> {

        public NoTimeWaster() {
        }

        @Override
        public long spendCPUTime( final LongValueMessage message ) {
            return 0;
        }
    }
}
